﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Tailoring
{
    public partial class frmcus : Form
    {
        public frmviewpemaish ParentForm { get; set; }
        private string clientid = "";
        private string name = "";
        private string mobile="";
        private string cnic="";
        private string procedurestr = "";
        private string dbperametr = "";
        private string orderID = "";
        private string ddate="";
        public frmcus()
        {
            InitializeComponent();
        }
        public string pidkey { set { clientid=value; } }
        public string pnamekey { set { name=value; } }
        public string pmobilekey { set{ mobile=value; } }
        public string pcnickey { set { cnic=value; } }
        public string ddatekey { set { ddate = value; } }
        SqlConnection conn = new SqlConnection("Server= localhost; database=tailoringmng; integrated Security=true");
        public SqlDbType Sqdbtype { get; set; }
        void delrecord()
        {
            {
                
                try
                {
                    conn.Open();
                                        
                    SqlCommand sqlcmd = new SqlCommand("delorder", conn);
                    sqlcmd.CommandType = CommandType.StoredProcedure;
                    sqlcmd.Parameters.AddWithValue("@orderid", Convert.ToInt32(orderID));
                    //sqlcmd.Parameters.AddWithValue("@orderid", Convert.ToInt32(orderID));
                    sqlcmd.ExecuteReader();
                    MessageBox.Show("Deletion Successfull",
                         "Record Deletion Confirmation",
                          MessageBoxButtons.OK,
                          MessageBoxIcon.Information);
                    
                    conn.Close();
                }

                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("An error occurred: {0}", ex.Message));
                }
            }
        }
        void popcmborder()
        { 
        
            try
            {
                conn.Open();
                string strquery = "select * from tblclientinfo where clientId=" + txtid.Text;
                SqlCommand sqlcmd = new SqlCommand(strquery, conn);
               
                SqlDataReader sqldreader = sqlcmd.ExecuteReader();
                if (sqldreader.HasRows)
                {
                    MessageBox.Show("This Customer Has");
                    while (sqldreader.Read())
                    {
                        
                    }
                }
                else
                {
                    
                }
                sqldreader.Close();
                conn.Close();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void searchloadcri()
        {
            if (clientid.Length > 0)
            {
                procedurestr = "viewpemaishbyid";
                dbperametr = "@id";
                Sqdbtype = SqlDbType.Int;
                dataloadcom(clientid, procedurestr, dbperametr, Sqdbtype);

            }
            if (name.Length > 0)
            {
                procedurestr = "viewpemaishbyname";
                dbperametr = "@name";
                Sqdbtype = SqlDbType.VarChar;
                dataloadcom(name, procedurestr, dbperametr, Sqdbtype);
            }
            if (mobile.Length > 0)
            {
                procedurestr = "viewpemaishbymobile";
                dbperametr = "@mobile";
                Sqdbtype = SqlDbType.VarChar;
                dataloadcom(mobile, procedurestr, dbperametr, Sqdbtype);
            }
            if (cnic.Length > 0)
            {
                procedurestr = "viewpemaishbycnic";
                dbperametr = "@cnic";
                Sqdbtype = SqlDbType.VarChar;
                dataloadcom(cnic, procedurestr, dbperametr, Sqdbtype);
            }
        
        }
        static string UppercaseWords(string value)
        {
            char[] array = value.ToCharArray();
            // Handle the first letter in the string.
            if (array.Length >= 1)
            {
                if (char.IsLower(array[0]))
                {
                    array[0] = char.ToUpper(array[0]);
                }
            }
            // Scan through the letters, checking for spaces.
            // ... Uppercase the lowercase letters following spaces.
            for (int i = 1; i < array.Length; i++)
            {
                if (array[i - 1] == ' ')
                {
                    if (char.IsLower(array[i]))
                    {
                        array[i] = char.ToUpper(array[i]);
                    }
                }
            }
            return new string(array);
        }
        public void updateOrderTask(string st)
        {
            try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                //conn.Open();
                SqlCommand cmd = new SqlCommand("updatesatus", conn);
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;

                 
                cmd.Parameters.AddWithValue("@orderStatus", st);
                cmd.Parameters.AddWithValue("@id", txtid.Text);
                cmd.Parameters.AddWithValue("@ddate", tsddate.Text);
                cmd.Parameters.AddWithValue("@oid", orderidstrip.Text);
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.InsertCommand = cmd;
                int a = adapter.InsertCommand.ExecuteNonQuery();
                cmd.Connection.Close();
                if (a == -1)
                {
                   MessageBox.Show( "Update Successfull Form Is Going to Close...");
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
            finally
            {
                this.conn.Close();
                this.Close();
            }

        }

        void dataloadcom(string searchkey, string procstr, string dbpar, SqlDbType dbtype)
        {
            try
            {

                //SqlConnection conn = new SqlConnection("Server= localhost; database=tailoringmng; integrated Security=true");
                string proce = procstr;
                SqlCommand cmd = new SqlCommand(proce, conn);
                cmd.CommandType = CommandType.StoredProcedure;


                cmd.Parameters.Add(new SqlParameter(dbpar, dbtype)).Value = searchkey;


                SqlDataAdapter da = new SqlDataAdapter(cmd);
                //SqlDataReader reader=reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                //cmd.Connection = conn;
                //if (conn == null && conn.State == ConnectionState.Closed)
                //{
                conn.Open();
                //}
                SqlDataReader reader = reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                if (reader.HasRows)
                {
                    int rowcount = 0;
                    while (reader.Read())
                    {
                        rowcount++;
                        txtid.Text = reader["ID"].ToString();
                        txtname.Text = reader["Name"].ToString();
                        txtmobile.Text = reader["Mobile"].ToString();
                        txtcity.Text = reader["city"].ToString();
                        txtaddress.Text = reader["address"].ToString();
                        txtcnic.Text = reader["cnic"].ToString();
                        txtother.Text = reader["other"].ToString();
                        txtlambai.Text = reader["lambai"].ToString();
                        txtchatti.Text = reader["chatti"].ToString();
                        txttera.Text = reader["tera"].ToString();
                        txtasteen.Text = reader["asteen"].ToString();
                        txtdaman.Text = reader["daman"].ToString();
                        txtshalwar.Text = reader["shalwar"].ToString();
                        txtpancha.Text = reader["pancha"].ToString();
                        txtcollar.Text = reader["pcollar"].ToString();
                        tsddate.Text = String.Format("{0:dd MMMM yyyy}", reader["DDate"]);
                        lbddayleft.Text = reader["Days_Left"].ToString();
                        ttstatus.Text = reader["Status"].ToString();
                        cmborderid.Items.Add(reader["orderId"].ToString());
                        orderID = reader["orderId"].ToString();
                        lbnoofcloths.Text = reader["Cloths No"].ToString();


                        pbox5kaj.Visible = reader.GetBoolean(reader.GetOrdinal("5Kajj"));
                        pbox2dagga.Visible = reader.GetBoolean(reader.GetOrdinal("Double Dagga"));
                        pboxsingles.Visible = reader.GetBoolean(reader.GetOrdinal("Single Salai"));
                        pbox2salai.Visible = reader.GetBoolean(reader.GetOrdinal("Double Salai"));
                        pboxwbtn.Visible = reader.GetBoolean(reader.GetOrdinal("waskatbtn"));
                        pboxvgalla.Visible = reader.GetBoolean(reader.GetOrdinal("vgalla"));
                        pboxgolkaf.Visible = reader.GetBoolean(reader.GetOrdinal("Gol Kaf"));
                        pboxkurta.Visible = reader.GetBoolean(reader.GetOrdinal("kurta"));
                        pboxsadakaf.Visible = reader.GetBoolean(reader.GetOrdinal("Sada Kaf"));
                        pboxsadapatti.Visible = reader.GetBoolean(reader.GetOrdinal("Sada Patti"));
                        pboxsamnayj.Visible = reader.GetBoolean(reader.GetOrdinal("Samnay Jaib"));
                        pboxshalwarjaib.Visible = reader.GetBoolean(reader.GetOrdinal("Shalwar Jaib"));
                        pboxsasteen.Visible = reader.GetBoolean(reader.GetOrdinal("Sada Asteen"));
                        pboxhbend.Visible = reader.GetBoolean(reader.GetOrdinal("Half Bend"));
                        pboxsjaib.Visible = reader.GetBoolean(reader.GetOrdinal("Side Jaib"));
                        pboxambend.Visible = reader.GetBoolean(reader.GetOrdinal("Am Bend"));
                        pboxcollar.Visible = reader.GetBoolean(reader.GetOrdinal("collar"));
                        pboxampatti.Visible = reader.GetBoolean(reader.GetOrdinal("ampatti"));
                       
                    }

                    //MessageBox.Show(string.Format("This Customer Has[ " + rowcount + "]Order", "Check All the Record", "Record Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Warning));

                }
                else
                {
                    this.Close();
                    MessageBox.Show("No record Found Search Again !");
                    searchdialog spop = new searchdialog();
                    spop.ShowDialog();

                }
          

                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
            finally { 
            
            }
            }

       public void dataloadbyddate()
        {
            try
            {

                
             
                SqlCommand cmd = new SqlCommand("pemishbyddate", conn);
                cmd.CommandType = CommandType.StoredProcedure;


                cmd.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int)).Value = clientid;
                cmd.Parameters.Add(new SqlParameter("@ddate", SqlDbType.VarChar)).Value = ddate;


                SqlDataAdapter da = new SqlDataAdapter(cmd);
             
                //if (conn == null && conn.State == ConnectionState.Closed)
                //{
                    conn.Open();
                //}
                SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                if (reader.HasRows)
                {
                    int rowcount = 0;
                    while (reader.Read())
                    {
                        rowcount++;
                        txtid.Text = reader["ID"].ToString();
                        txtname.Text = reader["Name"].ToString();
                        txtmobile.Text = reader["Mobile"].ToString();
                        txtcity.Text = reader["city"].ToString();
                        txtaddress.Text = reader["address"].ToString();
                        txtcnic.Text = reader["cnic"].ToString();
                        txtother.Text = reader["other"].ToString();
                        txtlambai.Text = reader["lambai"].ToString();
                        txtchatti.Text = reader["chatti"].ToString();
                        txttera.Text = reader["tera"].ToString();
                        txtasteen.Text = reader["asteen"].ToString();
                        txtdaman.Text = reader["daman"].ToString();
                        txtshalwar.Text = reader["shalwar"].ToString();
                        txtpancha.Text = reader["pancha"].ToString();
                        txtcollar.Text = reader["pcollar"].ToString();
                        tsddate.Text = String.Format("{0:dd MMMM yyyy}", reader["DDate"]);
                        lbddayleft.Text = reader["Days_Left"].ToString();
                        ttstatus.Text = reader["Status"].ToString();
                        cmborderid.Items.Add(reader["orderId"].ToString());
                        orderID = reader["orderId"].ToString();
                        lbnoofcloths.Text = reader["Cloths No"].ToString();
                        cmborderid.SelectedText = reader["orderId"].ToString();
                        orderidstrip.Text = reader["orderId"].ToString();
                       
                        pbox5kaj.Visible = reader.GetBoolean(reader.GetOrdinal("5Kajj"));
                        pbox2dagga.Visible = reader.GetBoolean(reader.GetOrdinal("Double Dagga"));
                        pboxsingles.Visible = reader.GetBoolean(reader.GetOrdinal("Single Salai"));
                        pbox2salai.Visible = reader.GetBoolean(reader.GetOrdinal("Double Salai"));
                        pboxwbtn.Visible = reader.GetBoolean(reader.GetOrdinal("waskatbtn"));
                        pboxvgalla.Visible = reader.GetBoolean(reader.GetOrdinal("vgalla"));
                        pboxgolkaf.Visible = reader.GetBoolean(reader.GetOrdinal("Gol Kaf"));
                        pboxkurta.Visible = reader.GetBoolean(reader.GetOrdinal("kurta"));
                        pboxsadakaf.Visible = reader.GetBoolean(reader.GetOrdinal("Sada Kaf"));
                        pboxsadapatti.Visible = reader.GetBoolean(reader.GetOrdinal("Sada Patti"));
                        pboxsamnayj.Visible = reader.GetBoolean(reader.GetOrdinal("Samnay Jaib"));
                        pboxshalwarjaib.Visible = reader.GetBoolean(reader.GetOrdinal("Shalwar Jaib"));
                        pboxsasteen.Visible = reader.GetBoolean(reader.GetOrdinal("Sada Asteen"));
                        pboxhbend.Visible = reader.GetBoolean(reader.GetOrdinal("Half Bend"));
                        pboxsjaib.Visible = reader.GetBoolean(reader.GetOrdinal("Side Jaib"));
                        pboxambend.Visible = reader.GetBoolean(reader.GetOrdinal("Am Bend"));
                        pboxcollar.Visible = reader.GetBoolean(reader.GetOrdinal("collar"));
                        pboxampatti.Visible = reader.GetBoolean(reader.GetOrdinal("ampatti"));
                 
                    }

                   

                }
                else
                {
                    MessageBox.Show("No record Found Search Again !");
                    this.Close();

                }
                

                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }
       public void accountinfo()
       {
           try
           {
               conn.Open();
               string qrystr = "Select * from tblaccount where ClientId= " + txtid.Text + " and orderId= " + orderidstrip.Text;
               SqlCommand acountcmd = new SqlCommand(qrystr,conn);
               acountcmd.CommandType = CommandType.Text;
               SqlDataAdapter actdr = new SqlDataAdapter(acountcmd);
               SqlDataReader acoountdr = acountcmd.ExecuteReader(CommandBehavior.CloseConnection);
               while (acoountdr.Read()) { 
               txtrece.Text=acoountdr["AmountRece"].ToString();
               txtamtbal.Text = acoountdr["AmountBal"].ToString();
               txttotalbal.Text = acoountdr["totalAmount"].ToString();
               
               }
               conn.Close();
           }
           catch(Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       
       
       }
        
        private void button1_Click(object sender, EventArgs e)
        {
            frmorder order = new frmorder();
            order.ShowDialog();
        }

        private void txtlambai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }

        private void frmcus_Load(object sender, EventArgs e)
        {
            
            
        }

        private void completeToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

       

        private void delateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you Sure Deleting This Order?",
                         "Record Deletion Confirmation",
                          MessageBoxButtons.YesNo,
                          MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                int cid = Convert.ToInt32(txtid.Text);
                delrecord();
                
            }
            
        }

        private void selectorder(object sender, EventArgs e)
        {
            try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                SqlCommand orderscmd = new SqlCommand("selectorder", conn);
                orderscmd.CommandType = CommandType.StoredProcedure;
                int ordrid = Convert.ToInt32(cmborderid.Text);
                orderscmd.Parameters.AddWithValue("@oId", ordrid);
                //SqlDataAdapter sqlorderr = new SqlDataAdapter(orderscmd);
                SqlDataReader orderReader = orderscmd.ExecuteReader();
                if (orderReader.HasRows)
                {
                    while (orderReader.Read())
                    {

                        tsddate.Text = String.Format("{0:dd MMMM yyyy}",orderReader["deliveryDate"]);
                        lbddayleft.Text = orderReader["Days_Left"].ToString();
                        ttstatus.Text = orderReader["odrstatus"].ToString();
                        //cmborderid.Items.Add(orderReader["orderId"].ToString());
                        //orderID = orderReader["orderId"].ToString();
                        lbnoofcloths.Text = orderReader["nubmerOfCloths"].ToString();
                        
                        //bool panjkaj = orderReader[5kaj].ToString();
                        //bool active = rdr.GetBoolean(rdr.GetOrdinal("Active"));
                        pbox5kaj.Visible = orderReader.GetBoolean(orderReader.GetOrdinal("panjKaj"));
                        pbox2dagga.Visible = orderReader.GetBoolean(orderReader.GetOrdinal("doubleDagga"));
                        pboxsingles.Visible = orderReader.GetBoolean(orderReader.GetOrdinal("singleSalai"));
                        pbox2salai.Visible = orderReader.GetBoolean(orderReader.GetOrdinal("doubleSalai"));
                        pboxwbtn.Visible = orderReader.GetBoolean(orderReader.GetOrdinal("waskatbtn"));
                        pboxvgalla.Visible = orderReader.GetBoolean(orderReader.GetOrdinal("vgalla"));
                        pboxgolkaf.Visible = orderReader.GetBoolean(orderReader.GetOrdinal("golKaf"));
                        pboxkurta.Visible = orderReader.GetBoolean(orderReader.GetOrdinal("kurta"));
                        pboxsadakaf.Visible = orderReader.GetBoolean(orderReader.GetOrdinal("sadaKaf"));
                        pboxsadapatti.Visible = orderReader.GetBoolean(orderReader.GetOrdinal("sadaPatti"));
                        pboxsamnayj.Visible = orderReader.GetBoolean(orderReader.GetOrdinal("samnayJaib"));
                        pboxshalwarjaib.Visible = orderReader.GetBoolean(orderReader.GetOrdinal("shalwarJaib"));
                        pboxsasteen.Visible = orderReader.GetBoolean(orderReader.GetOrdinal("sadaAsteen"));
                        pboxhbend.Visible = orderReader.GetBoolean(orderReader.GetOrdinal("halfBend"));
                        pboxsjaib.Visible = orderReader.GetBoolean(orderReader.GetOrdinal("sideJaib"));
                        pboxambend.Visible = orderReader.GetBoolean(orderReader.GetOrdinal("aambend"));
                        pboxcollar.Visible = orderReader.GetBoolean(orderReader.GetOrdinal("collar"));
                        pboxampatti.Visible = orderReader.GetBoolean(orderReader.GetOrdinal("ampatti"));
                    }

                }
                this.Refresh();
                orderReader.Close();
               
             conn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);

                }
        }

        private void placeOrderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmorder order = new frmorder();
            order.MdiParent = this.ParentForm;
            order.ShowDialog();
            this.Close();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you Sure to Close the Form?",
                         "Customer  Application Closing Confirmation",
                          MessageBoxButtons.YesNo,
                          MessageBoxIcon.Information) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void doneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string statusor=tmdone.Tag.ToString();
            accountinfo();
            updateOrderTask(statusor);
            //mdistrip mdis = new mdistrip();
            
            ////mdis.Show();
            ////mdis.MdiParent = this.ParentForm;
            //mdis.loaddata();
            //mdis.loaddatadg2();
            
        }

        private void customerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmviewpemaish frm = new frmviewpemaish();
            string cname = UppercaseWords(txtname.Text);


            frm.pid = txtid.Text;
            frm.pname = txtname.Text;
            frm.pmobile = txtmobile.Text;
            frm.pcnic = txtcnic.Text;
            frm.paddress = txtaddress.Text;
            frm.pchati = txtchatti.Text;
            frm.plambai = txtlambai.Text;
            frm.pdaman = txtdaman.Text;
            frm.pasteen = txtasteen.Text;
            frm.ptera = txttera.Text;
            frm.pcollor = txtcollar.Text;
            frm.pshalwar = txtshalwar.Text;
            frm.ppansa = txtpancha.Text;
            frm.pcity = txtcity.Text;
            frm.Mnupdate.Enabled = true;
            frm.Mnedit.Enabled = false;
            frm.ShowDialog();
            this.Close();
        }

        private void pemaishToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void orderToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }


        }
}
